<?php

class M_agenda extends CI_Model {

	function __construct(){
		parent::__construct();
	}

	function get_view($judul){

		$sql="SELECT agenda,
					tanggal,
					date_format(tanggal,'%d-%m-%Y') as tanggal_ina,
					dayname(tanggal) as dayname,
					tempat,
					jam

					FROM agenda WHERE lower(trim(agenda))=trim('".strtolower($judul)."')";
		$this->firephp->log($sql);
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->row();
		}else{
			return "";
		}
	}

	function getDetail($id){
		$this->db->where('id', $id);
		$query = $this->db->get('agenda');

		$num_rows = $query->num_rows();
		if($num_rows > 0){
			return $query->row();
		}
	}

	function agenda_get_count_rows($query){
		$sql="SELECT * FROM agenda

				WHERE publish='1'";
		$num_rows = $this->m_public_function->get_count_rows($sql);
		return $num_rows;
	}

	function agenda_listview($query, $start, $end){

		$sql="SELECT agenda,
					 tanggal,
					 date_format(tanggal,'%d-%m-%Y') tanggal_ina,
					 dayname(tanggal) dayname
					 FROM agenda

				WHERE publish='1' ORDER BY tanggal DESC";

		$num_rows = $this->m_public_function->get_count_rows($sql);
		$sql.=" LIMIT $start, $end";
		//$this->firephp->log($sql);

		$query = $this->db->query($sql);
		if($query->num_rows()>0){
			return $query->result();
		}else{
			return null;
		}

	}

	function agenda_list($search_text, $start, $end, $tglawal, $tglakhir){

		$sql="SELECT
				agenda.id,
				agenda,
				tempat,
				date_format(tanggal,'%d-%m-%Y') as tanggal,
				jam,
				publish
			FROM
				agenda
			WHERE agenda.id IS NOT NULL 
			";
		if($search_text != ''){
			$sql .= " AND ( lower(agenda) LIKE '%".$search_text."%' 
			)";
		}
		if($tglawal != '' && $tglakhir != ''){
			$sql .= " AND agenda.tanggal >= '".$tglawal."' AND agenda.tanggal <= '".$tglakhir."' ";
		}
		$num_rows = $this->m_public_function->get_count_rows($sql);
		if($start != 0 && $end != 0){
			$sql.=" LIMIT $start, $end";
		}
		$query = $this->db->query($sql);

		if($num_rows > 0){
			$result = $query->result();
			$i = 0;
			foreach($result as $row){
				$data[$i] = $row;
				$i++;
			}
		}
		$final[] = array(
			"TotalRows"=>$num_rows,
			"results"=>$data
		);
		return json_encode($final);
	}

	function agenda_save($params){
		extract($params);
		if($task == 'create'){
			$this->db->insert('agenda',$data);
		}else{
			$this->db->where('id', $id);
			$this->db->update('agenda',$data);
		}
		$this->firephp->log($this->db->last_query());
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

	function agenda_delete($id){
		$this->db->where('id',$id);
		$this->db->delete('agenda');
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	function agenda_switchpublish($id, $stats){
		$data = array(
			"publish"=>$stats
		);
		$this->db->where("id", $id);
		$this->db->update('agenda', $data);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

}